package com.view;

import com.bean.User;
import com.service.RecordService;
import com.service.UserService;
import com.service.impl.RecordServiceImpl;
import com.service.impl.UserServiceImpl;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends JFrame {
    private Container contentP;

    private JLabel titleLab;

    private JLabel nameLab;

    private JLabel pwdLab;

    private JTextField nameTxt;

    private JPasswordField pwdTxt;

    private JButton loginBtn;

    private UserService userService = new UserServiceImpl();

    private RecordService recordService = new RecordServiceImpl();

    public LoginFrame(){
        Toolkit tk = Toolkit.getDefaultToolkit();
        this.setIconImage(tk.createImage("image/hp.JPG"));
        this.setTitle("销售管理系统");
        this.setLocation(440, 330);
        this.setSize(400, 300);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.init();

        this.setVisible(true);
    }

    public void init() {
        this.contentP = this.getContentPane();
        this.contentP.setBackground(Color.WHITE);
        this.contentP.setLayout(null);

        this.titleLab = new JLabel();
        this.titleLab.setText("进销存系统");
        this.titleLab.setFont(new Font("微软雅黑",1, 28));
        this.titleLab.setBounds(100, 0, 200, 50);
        this.contentP.add(this.titleLab);


        this.nameLab = new JLabel();
        this.nameLab.setText("用户名：");
        this.nameLab.setFont(new Font("微软雅黑", 1, 14));
        this.nameLab.setBounds(50, 70, 80, 30);
        this.contentP.add(this.nameLab);

        this.pwdLab = new JLabel();
        this.pwdLab.setText("密   码：");
        this.pwdLab.setFont(new Font("微软雅黑", 1, 14));
        this.pwdLab.setBounds(50, 130, 80, 30);
        this.contentP.add(this.pwdLab);


        this.nameTxt = new JTextField();
        this.nameTxt.setFont(new Font("微软雅黑", Font.ITALIC, 18));
        this.nameTxt.setBounds(135, 70, 150, 30);
        this.contentP.add(this.nameTxt);
        nameTxt.setText("admin");

        this.pwdTxt = new JPasswordField();
        this.pwdTxt.setFont(new Font("微软雅黑", Font.ITALIC, 18));
        this.pwdTxt.setBounds(135, 130, 150, 30);
        this.contentP.add(this.pwdTxt);
        pwdTxt.setText("123456");

        this.loginBtn = new JButton();
        this.loginBtn.setText("登录");
        this.loginBtn.setFont(new Font("微软雅黑", Font.BOLD, 18));
        this.loginBtn.setBounds(235, 200, 80, 30);
        this.contentP.add(this.loginBtn);

        this.loginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String account = nameTxt.getText();
                String password =String.valueOf(pwdTxt.getPassword());
                System.out.println(account);
                if(userService.accountPasswordIsEqual(account,password)){
                    User user = new User();
                    user = userService.returnUser(account);

                    recordService.addRecord(user);

                    MainFrame mainFrame = new MainFrame(user);
                    mainFrame.setVisible(true);
                    LoginFrame.this.dispose();
                }else {
                    JOptionPane.showMessageDialog(null,"账号密码错误");
                }
            }
        });
    }
}
